package com.atguigu.gulimall.seckill.controller;

import com.atguigu.common.to.SeckillSkuTO;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.seckill.service.SeckillService;
import com.atguigu.gulimall.seckill.to.SeckillSkuRedisTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author baiWT
 * @Description
 * @Date 2022/4/14 22:05
 **/
@Api("秒杀服务")
@RestController
@RequestMapping("/seckill")
public class SeckillController {

    @Resource
    private SeckillService seckillService;

    /**
     * 展示当前正在秒杀的商品
     * @return
     */
    @ApiOperation(value = "展示当前正在秒杀的商品")
    @GetMapping("/current")
    public R getCurrentSeckillSessions() {
        List<SeckillSkuRedisTO> skus = seckillService.currentSeckill();
        return R.ok().setData(skus);
    }

    /**
     * 查询商品是否参与秒杀
     * @param skuId
     * @return
     */
    @ApiOperation(value = "查询商品是否参与秒杀",notes = "根据商品skuId查询")
    @ApiImplicitParam(name = "skuId",value = "商品skuId",required = true,dataType = "Long",paramType = "path")
    @GetMapping("/getSeckillSkuInfo/{skuId}")
    public R getSeckillSkuInfo(@PathVariable("skuId") Long skuId){
        SeckillSkuTO to = seckillService.getSeckillSkuInfo(skuId);
        return R.ok().setData(to);
    }

    /**
     * 秒杀下单
     * @param seckillId
     * @param seckillToken
     * @param count
     * @param model
     * @return
     */
    @ApiOperation(value = "秒杀下单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "seckillId",value = "秒杀id",required = true,dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "seckillToken",value = "秒杀token标识",required = true,dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "count",value = "数量",required = true,dataType = "String",paramType = "body")
    })
    @GetMapping("/item")
    public String seckillItem(@RequestParam("seckillId") String seckillId,
                              @RequestParam("seckillToken") String seckillToken,
                              @RequestParam("count") Integer count,
                              Model model)  {
        // 秒杀成功，订单号
        String orderSn = seckillService.seckillItem(seckillId, seckillToken, count);
        model.addAttribute("orderSn", orderSn);
        return "success";
    }
}
